import { defineStore } from "pinia";
import { marked } from "marked";

interface Message {
  role: string;
  content: string;
  status: number;
}

export const userMsgStore = defineStore("userMsgStore", {
  state: () => ({
    list: [] as Message[],
    loading: false,
    aiCol_id: "",
  }),
  actions: {
    //加载
    setLoading(status: boolean) {
      this.loading = status;
    },

    //获取id
    setAiCol_id(id: string) {
      this.aiCol_id = id;
    },

    //添加信息
    setList(list) {
      this.list = list;
    },

    userAddMsg(msg: string) {
      this.list.push({
        role: "user",
        content: msg,
        status: 2,
      });
    },
    aiAddMsg(content: string, status: number) {
      const runMsg = this.list.find((i) => i.status !== 2);
      if (!runMsg) {
        this.list.push({
          role: "assistant",
          content: content,
          status: status,
        });
      } else {
        runMsg.content += content;
        runMsg.status = status;
        if (status === 2) {
          runMsg.content = marked(runMsg.content);
        }
      }
    },

    clear() {
      this.list = [];
    },
  },
});
